<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class provides access to data layer.
 *
 * @package Aurorum
 * @subpackage Models
 */

class ADS_PlayerModel extends ADS_DefaultModel {

    public function countPlayers() {
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_players]
          %if', (bool)$this->useFilter, 'WHERE %and', $this->filter
        )->cnt;
    }
    
    public function getPlayers() {
        return dibi::fetchAll('
          SELECT            
            *
          FROM
            [:wp:ads_players] AS t1
           %if', (bool)$this->useFilter, 'WHERE %and', $this->filter, '%end',
          '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy, '%end',
          '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset
        );
    }
     
    public function getPlayer($playerId) {
        if(!is_int($playerId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT            
            *
          FROM
            [:wp:ads_players] AS t1
          WHERE
            playerId = %i
        ', $playerId);
    }
    
    public function getCommands($playerId) {
        if(!is_int($playerId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetchAll('
          SELECT            
            *
          FROM
            [:wp:ads_commands] AS t1
          WHERE
            playerId = %i
        ', $playerId);
    }
    
    public function checkClientKey($clientKey) {
        if(!is_string($clientKey)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT            
            *
          FROM
            [:wp:ads_players] AS t1
          WHERE
            clientKey = %s
        ', $clientKey)?true:false;
    }
    
    public function resolveClientKey($clientKey) {
        if(!is_string($clientKey)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT            
            *
          FROM
            [:wp:ads_players] AS t1
          WHERE
            clientKey = %s
        ', $clientKey);
    }
    
    public function addPlayer($player){
        if(!is_array($player)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        dibi::query('INSERT INTO [:wp:ads_players]', $player);
    }
    
    public function editPlayer($playerId, $player){
        if(!is_array($player) || !is_int($playerId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }

        dibi::query('UPDATE [:wp:ads_players] SET', $player, 'WHERE playerId = %i', $playerId);
    }
    
    public function updateState($clientKey, $state){
        if(!is_array($state) || !is_string($clientKey)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }

        dibi::query('UPDATE [:wp:ads_players] SET', $state, 'WHERE clientKey = %s', $clientKey);
    }
    
    public function removePlayer($playerId){
        if(!is_int($playerId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        $player = $this->getPlayer($playerId);
        //update positions
        dibi::query('DELETE FROM [:wp:ads_players] WHERE playerId = %i LIMIT 1', $playerId);
    }
    
    public function confirmCommands($playerId){
        if(!is_int($playerId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        //update positions
        dibi::query('DELETE FROM [:wp:ads_commands] WHERE playerId = %i', $playerId);
    }

}
